#pragma once

#include "ColligereDatabaseTreeCtrlBase.hpp"
#include "ColligereAudioDatabaseHandler.hpp"

////////////////////////////////////////////////////
// col::AlbumTreeItemData
////////////////////////////////////////////////////

namespace col
{

class AlbumTreeItemData : public TreeItemData
{
public:
	AlbumTreeItemData();
	AlbumTreeItemData ( DatabaseItemId artistId, 
		DatabaseItemId albumId, const wxString &rYear );

	virtual ~AlbumTreeItemData();

	DatabaseItemId GetArtistId() const;
	DatabaseItemId GetAlbumId() const;
	unsigned long GetYear() const;

	bool operator == ( const AlbumTreeItemData &rData ) const;
private:
	DatabaseItemId mArtistId, mAlbumId;
	unsigned long mYear;
};

//////////////////////////////////////////////
// col::ArtistTreeItemData
//////////////////////////////////////////////

class ArtistTreeItemData : public TreeItemData
{
public:
	ArtistTreeItemData ( const Artist &rArtist );

	virtual ~ArtistTreeItemData();

	wxUint32 GetArtistId() const;
	const wxString &GetArtist() const;
	const wxString &GetCountry() const;

	bool operator == ( const ArtistTreeItemData &rData ) const;
private:
	const Artist &mrArtist;
};

}

//////////////////////////////////////////////
// ColligereAudioDatabaseTreeCtrl
//////////////////////////////////////////////

//! A tree control for showing items from an audio collection database.
class ColligereAudioDatabaseTreeCtrl : public col::DatabaseTreeCtrlBase
{
public:
	ColligereAudioDatabaseTreeCtrl();

	virtual ~ColligereAudioDatabaseTreeCtrl();

	/*! @brief Initializes the control.
	 *
	 * Connects event handlers and loads the tree item images.
	 **/
	void Initialize();

	wxTreeItemId AppendItem ( const col::DatabaseItemBase &rItem, bool select = false );
	wxTreeItemId UpdateItem ( const col::DatabaseItemBase &rItem );	
	void DeleteItem ( const col::TreeItemData &rItemData );
	void SelectItem ( const col::DatabaseItemBase &rItem, bool select );

	//! Removes all items from the tree except the root item.
	void ClearItemList();

	struct BrowseBy 
	{ 
		enum { Artist = 1, Year, Genre, Country, Format, Tag }; 
	};
private:
	wxTreeItemId AppendInBrowseByArtistMode ( const wxTreeItemId &rRoot,
		const col::Album &rAlbum );
	
	wxTreeItemId AppendInBrowseByCountryMode ( const wxTreeItemId &rRoot,
		const col::Album &rAlbum );
	
	wxTreeItemId AppendInBrowseByFormatMode ( const wxTreeItemId &rRoot,
		const col::Album &rAlbum );
	
	wxTreeItemId AppendInBrowseByGenreMode ( const wxTreeItemId &rRoot,
		const col::Album &rAlbum );
	
	wxTreeItemId AppendInBrowseByYearMode ( const wxTreeItemId &rRoot,
		const col::Album &rAlbum );
	
	wxTreeItemId AppendInBrowseByTagMode ( const wxTreeItemId &rRoot, 
		const wxString &rTag, const col::Album &rAlbum );

	wxTreeItemId FindArtist ( const wxTreeItemId &rRoot, 
		const wxString &rArtist, const wxString &rCountry );
	
	wxTreeItemId FindAlbum ( const wxTreeItemId &rRoot, 
		const col::AlbumTreeItemData &rItemData );

	wxInt32 GetImageIndexForType ( const wxString &rType ) const;

	const wxString FormatToDefaultIfEmpty ( const wxString &rValue );
	const wxString FormatToDefaultIfZero ( const wxString &rValue );

	const wxString FormatAlbumToText ( const col::Album &rAlbum ) const;

	bool IsArtistItem ( const wxTreeItemId &rItem ) const;
	bool IsAlbumItem ( const wxTreeItemId &rItem ) const;

	wxInt32 OnCompareItems ( const wxTreeItemId &rItem1, const wxTreeItemId &rItem2 );

	//////////////////////////////////////
	// Event handlers
	//////////////////////////////////////

	void OnItemExpanding ( wxTreeEvent &rEvent );
	void OnItemCollapsing ( wxTreeEvent &rEvent );
	void OnGetItemToolTip ( wxTreeEvent &rEvent );

	typedef std::map < wxString, wxInt32 > ImageMap;

	ImageMap mImageList;

//public:
	DECLARE_DYNAMIC_CLASS ( ColligereAudioDatabaseTreeCtrl );
};